package erlangToJsPackage;

import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import structurePackage.Donnee;
import structurePackage.Noeud;

/**
 * Librairie JS pour créer des Ring  :
 * http://labs.unwieldy.net/moowheel/
 *
 */
public class Generator {
	ArrayList<Noeud> nodes=new ArrayList<Noeud>();
	String entete = 
			"<html><head><script type='text/javascript' src='mootools-1.2-core-nc.js'>"+
					"</script><script type='text/javascript' src='mootools-1.2-more.js'>"+
					"</script><script type='text/javascript' src='excanvas.js'></script>"+
					"<script type='text/javascript' src='canvastext.js'></script>"+
					"<script type='text/javascript' src='moowheel.js'></script>"+
					"</head>"+
					"<body style='background:black;''>test"+
					"<center id='canvas-container'></center>"+
					"<script>";

	String pied = 
			"</script><script>"+
					"var wheel = new MooWheel(data, $('canvas-container'));"+
					"</script></body></html>";

	String centre = "";

	/**
	 * Création des tableaux ditems pour définir les relations entre noeuds : 
	 * La représentation est de la forme :
	 * var data = [{
             id: 'joeb',
             text: 'Joe B',
             connections: ['ryank', 'charliec']
            },

            {
             id: 'ryank',
             text: 'Ryan K',
             connections: ['charliec']
            }];
	 * @param nodes
	 */
	public Generator(ArrayList<Noeud> nodes){
		System.out.println("Creation d'un Generator avec la liste de noeuds : ");
		/*for(int i=0;i<nodes.size();i++){
			System.out.println(nodes.get(i));
		}*/
		this.nodes=nodes;
		StringBuffer sb = new StringBuffer();
		sb.append("var data=[");
		int size = nodes.size();
		for(int i=0;i<size;++i){
			Noeud noeud = nodes.get(i);
			String nom = noeud.nom;

			String pred,suiv;	
			
			Noeud npred = nodes.get((i-1+size)%size);
			if(noeud.donnees.size()==0)
				pred = npred.nom.toLowerCase();
			else
				pred = "data"+noeud.donnees.get(noeud.donnees.size()-1).numero;
			
			Noeud nsuiv = nodes.get((i+1+size)%size);
			
			if(nsuiv.donnees.size()==0){
				suiv = nsuiv.nom.toLowerCase();
			}
			else{
				//suiv = "data"+nsuiv.donnees.get(0).numero;
				suiv="";
			}
			
			int size2 = noeud.donnees.size();
			for(int j=0;j<size2;++j){
				Donnee donnee = noeud.donnees.get(j);
				sb.append("{id:'").append("data"+donnee.numero).append("',");
				sb.append("text:'[").append(donnee.toString()).append("]',");
				sb.append("connections:['");
				String p,s;
				if(j==0){
					//p=npred.nom.toLowerCase();
				}
				else{
					p="data"+noeud.donnees.get(j-1).numero;
					sb.append(p).append("','");
				}
				if(j==size2-1){
					s=noeud.nom.toLowerCase();
				}
				else{
					s="data"+noeud.donnees.get(j+1).numero;
				}
				sb.append(s);

				sb.append("']");
				sb.append("},");
			}
			
			sb.append("{id:'").append(nom.toLowerCase()).append("',");
			sb.append("text:'                ").append(nom.toUpperCase()).append("                ',");
			sb.append("connections:['");
			sb.append(pred).append("','").append(suiv).append("'");

			for(String finger : noeud.fingers){
				sb.append(",'").append(finger).append("'");
			}
			
			sb.append("]");
			sb.append("}");
			
			if(i != size-1)
				sb.append(",");
		}
		sb.append("];");
		centre = sb.toString();
	}

	public String toHtml(){
		return entete+centre+pied;
	}
	public void afficher(){
		if(java.awt.Desktop.isDesktopSupported()){
			System.out.println("DeskTop Supported");
			Generator g = new Generator(nodes);
			String page = g.toHtml();

			try {
				PrintWriter pw = new PrintWriter(new File("out.html"));
				pw.println(page);
				pw.close();
			} catch (IOException e1) {
				e1.printStackTrace();
			}

			try {
				Desktop.getDesktop().open(new File("out.html"));
			} catch (IOException e) {
				//e.printStackTrace();
			}
		}else {
			System.out.println("Ouvrez le fichier [out.html] dans votre navigateur");
		}
	}
}
